<!-- 米厂袋皮库存明细列表 -->
<template>
  <div>
    <div
      class="bag_detail_management"
      v-if="!appStore.isPhone">
<!--      <SearchCard-->
<!--          ref="searchCardRef"-->
<!--          :searchTemplate="searchTemplate"-->
<!--          :more="true"-->
<!--          @add-event="addEvent"-->
<!--          @reset="getList"-->
<!--          @search="(data) => getList(data)" />-->
      <el-card
        class="search_card"
        ref="searchCardRef">
        <el-form
          :model="formData"
          class="search_form">
          <el-row>
            <el-form-item label="米厂">
              <Cascader
                ref="cascaderRef"
                :checkStrictly="true"
                @change="change" />
            </el-form-item>
            <el-form-item label="启用状态">
              <el-select
                v-model="formData.isEnable"
                placeholder="启用状态"
                style="width: 100px"
                clearable>
                <el-option
                  :label="config.button.text.enable"
                  :value="true" />
                <el-option
                  :label="config.button.text.stop"
                  :value="false" />
              </el-select>
            </el-form-item>
            <el-button
              type="primary"
              @click="getList(formData)"
              class="query_button">
              <template #icon>
                <el-icon>
                  <search />
                </el-icon>
              </template>
              {{ config.button.text.query }}
            </el-button>
            <el-button @click="reset">
              <template #icon>
                <el-icon>
                  <Refresh />
                </el-icon>
              </template>
              {{ config.button.text.reset }}
            </el-button>
          </el-row>
        </el-form>
        <div style="display: flex;">
          <el-button
            v-permission="validateButtonPermission(route, 0)"
            type="info"
            @click="bePutIn">
            <template #icon>
              <el-icon>
                <Plus />
              </el-icon>
            </template>
            入库
          </el-button>
          <el-button
            v-permission="validateButtonPermission(route, 3)"
            type="danger"
            @click="bePutOut">
            <template #icon>
              <el-icon>
                <Minus />
              </el-icon>
            </template>
            扣减
          </el-button>
        </div>
      </el-card>
      <el-card class="table_card">
        <Table
          :data="tableData"
          v-loading="tableLoading"
          :height="height">
          <el-table-column
            prop="baseRiceBarnSimpleName"
            show-overflow-tooltip
            label="米厂简称"
            min-width="120"
            :resizable="false"
          />
          <el-table-column
            prop="baseRiceBarnGoodsSimpleName"
            show-overflow-tooltip
            min-width="120"
            label="商品简称"
            :resizable="false"
          />
          <el-table-column
            prop="bagInfoSimpleName"
            show-overflow-tooltip
            min-width="120"
            label="袋皮简称"
            :resizable="false"
          />
          <el-table-column
            prop="count"
            min-width="100"
            label="扣减/入库"
            show-overflow-tooltip
            :resizable="false"
          />
          <el-table-column
            prop="dataSourceEnum"
            min-width="180"
            show-overflow-tooltip
            label="信息来源"
            :resizable="false"
          />
          <el-table-column
            prop="remark"
            label="备注"
            min-width="120"
            show-overflow-tooltip
            :resizable="false" />
          <el-table-column
            label="启用状态"
            min-width="100"
            :resizable="false">
            <template #default="scope">
              <el-tag :type="scope.row.isEnable ? 'primary' : 'danger'">
                {{ scope.row.isEnable ? config.button.text.enable
                  : config.button.text.stop }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="160"
            fixed="right"
            :resizable="false">
            <template #default="scope">
              <el-button
                type="warning"
                :disabled="scope.row.bagAlterTypeEnum=='INITIALIZE_INVENTORY'"
                v-permission="validateButtonPermission(route, 1)"
                @click="editEvent(scope.row)">
                {{ config.button.text.edit }}
              </el-button>
              <el-button
                type="success"
                @click="viewEvent(scope.row)">
                {{ config.button.text.view }}
              </el-button>
            </template>
          </el-table-column>
        </Table>
        <Pagination
          :pageNum="pageNum"
          :pageSize="pageSize"
          :total="total"
          @handle-size-change="(val) => pageSize = val"
          @handle-current-change="(val) => pageNum = val" />
      </el-card>
      <!-- 查看 -->
      <View
        ref="viewRef"
        :rowData="rowData" />
      <!-- 修改 -->
      <Edit
        ref="editRef"
        :rowData="rowData"
        @save="save" />
      <!-- 入库 -->
      <BePutIn
        ref="putInRef"
        @save="save" />
      <!-- 扣减 -->
      <BePutOut
        ref="putOutRef"
        @save="save" />
    </div>
    <!-- 手机端页面 -->
    <h5Index v-else></h5Index>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from "vue";
import Pagination from "@/components/Pagination.vue"
import Table from "@/components/Table.vue"
import ApiBagInventory from "@/api/modules/bag_inventory"
import { useTable } from "@/hooks/useTable"
import View from "./component/View.vue"
import Edit from "./component/Edit.vue"
import h5Index from "./h5Index.vue";
import BePutIn from "./component/BePutIn.vue"
import BePutOut from "./component/BePutOut.vue"
import message from "@/common/element-plus/message";
import config from "@/common/element-plus/config";
import app from "@/common/config/app";
import { validateButtonPermission } from "@/common/utils/validate";
import { useRoute } from "vue-router"
import { useAppStore } from "@/stores/app"
import { storeToRefs } from "pinia";
import Cascader from "@/components/Cascader.vue"
import SearchCard from "@/components/SearchCard.vue"


const route = useRoute()
// 查询表单
const formData = ref({})
// 查看组件实例
const viewRef = ref()
// 新增修改组件实例
const editRef = ref()
//入库组件实例
const putInRef = ref()
//扣减组件实例
const putOutRef = ref()
// // 表格每行数据
const rowData = ref({})
// 表格高度(固定表头)
const height = ref(0)
// 查询卡片实例
const searchCardRef = ref()
// 级联选择框实例
const cascaderRef = ref()

// 查询模板
const searchTemplate = ref([
  {
    label: "资金仓库名称",
    type: "simpleSelect",
    placeholder: "资金仓库名称",
    interfaceUrl: "baseCapitalWarehouse",
    value: "baseCapitalWarehouseId",
    isClosed: false,
  },
  {
    label: "商品名称",
    type: "input",
    placeholder: "请输入商品名称",
    value: "goodsName",
    isClosed: false,
  },
  {
    label: "是否完成提货",
    type: "yesNo",
    placeholder: "选择",
    value: "isFinishPick",
    isClosed: false,
  },
  {
    label: "车牌号",
    type: "input",
    placeholder: "请输入车牌号",
    value: "carNumber",
    isClosed: true,
  },
  {
    label: "司机名称",
    type: "input",
    placeholder: "请输入司机名称",
    value: "driverName",
    isClosed: true,
  },
  {
    label: "创建日期",
    type: "daterange",
    value: "date",
    isClosed: true,
  },
]);

const riceFactorySimpleNameList = ref([])
const goodsSimpleNameList = ref([])

const { tableMethods, tableState, pagination } = useTable({
  fetchDataApi: async (data) => {
    const { pageSize, pageNum } = pagination
    const res = await ApiBagInventory.selectPage(data, {
      pageNum: pageNum.value,
      pageSize: pageSize.value,
    })
    return {
      list: res.data.records,
      total: res.data.total
    }
  }
})
const { getList } = tableMethods
const { pageSize, pageNum } = pagination
const { total, tableLoading, tableData } = tableState

//获取米厂简称列表
const getRiceFactorySimpleNameList = async () => {
  const data = await ApiBagInventory.getSimpleNameList("/baseRiceBarn/")
  riceFactorySimpleNameList.value = data.data
}

const change = (data) => {
  formData.value.baseRiceBarnId = data[0]
  formData.value.baseRiceBarnGoodsId = data[1]
}

//根据米厂获取商品列表
const getGoodsSimpleNameList = async (id) => {
  const data = await ApiBagInventory.getGoodsSimpleNameList("/baseRiceBarnGoods/",id)
  goodsSimpleNameList.value = data.data
}

// 重置
const reset = () => {
  formData.value = {}
  cascaderRef.value.selectValue = ""
  getList()
}

// 打开查看对话框
const viewEvent = (row) => {
  viewRef.value.visibility = true
  rowData.value = row
}

// 打开新增对话框
// const addEvent = () => {
//   editRef.value.visibility = true
//   rowData.value = {}
// }

// 打开入库对话框
const bePutIn = () => {
  putInRef.value.visibility = true
}

// 打开扣减对话框
const bePutOut = () => {
  putOutRef.value.visibility = true
}

// 打开修改对话框
const editEvent = (row) => {
  editRef.value.visibility = true
  rowData.value = Object.assign({}, row)
}

// 保存后回调
const save = async () => {
  putOutRef.value.visibility = false
  putInRef.value.visibility = false
  editRef.value.visibility = false
  message.success()
  await getList()
}

// 监听查询卡片大小变化
const resizeObserver = new ResizeObserver(entries => {
  for (const entry of entries) {
    if (contentFullScreen.value) {
      height.value = (window.innerHeight - app.otherHeight - app.tagHeight - entry.contentRect.height) + "px"
    } else {
      height.value = (window.innerHeight - app.otherHeight - app.tagHeight - app.headerHeight - entry.contentRect.height) + "px"
    }
  }
})

watch(() => formData.value.baseRiceBarnId, (newVal) => {
  if (newVal) {
    getGoodsSimpleNameList(newVal)
  } else {
    goodsSimpleNameList.value = [];
  }
}, { immediate: true })

const appStore = useAppStore()

// 保持响应式
const { contentFullScreen } = storeToRefs(appStore)

onMounted(() => {
  if (appStore.isPhone) return
  resizeObserver.observe(searchCardRef.value.$el)
  getRiceFactorySimpleNameList()
})
</script>

<style lang="scss" scoped>
.bag_detail_management {
  .search_card {
    margin-bottom: 4px;

    :deep(.el-card__body) {
      padding: 10px;
      display: flex;
      justify-content: space-between;
    }

    .el-button {
      margin: 5px 10px 5px 0;
    }

    .query_button {
      margin-left: 100px;
    }

    .el-form-item {
      margin-right: 10px;
      padding: 5px 0;
    }
  }

  .table_card {
    :deep(.el-card__body) {
      padding: 0;
    }
  }
}
</style>
